/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.sys.web;

import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.modules.sys.entity.Areas;
import com.thinkgem.jeesite.modules.sys.entity.Office;
import com.thinkgem.jeesite.modules.sys.entity.TbCommunity;
import com.thinkgem.jeesite.modules.sys.entity.User;
import com.thinkgem.jeesite.modules.sys.service.*;
import com.thinkgem.jeesite.modules.sys.utils.UserUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;


/**
 * 物业管理员
 * 是维护物业管理员的cotroller
 */
@Controller
@RequestMapping(value = "${adminPath}/sys/tenementManagerController")
public class TenementManagerController extends BaseController {

	@Autowired
	private SystemService systemService;
	@Autowired
	PC service;// 调用业务层方法
	@Autowired
	private TenementManagerService tenementManagerService;
	@Autowired
	private ShopAddService shopAddService;
	@Autowired
	private TbCommunityService tbCommunityService;
	@ModelAttribute
	public User get(@RequestParam(required=false) String id) {
		if (StringUtils.isNotBlank(id)){
			return systemService.getUser(id);
		}else{
			return new User();
		}
	}
	/**
	 * 查询得到代理商下的物业管理员列表，即店铺信息列表
	 * @param user
	 * @param request
	 * @param response
	 * @param model
     * @return
     */
	@RequestMapping("/tenenmentManagerList")
	public String list(User user, HttpServletRequest request, HttpServletResponse response, Model model) {
		user.setAgentId(UserUtils.getUser().getId());
		Page<User> page = tenementManagerService.findUser(new Page<User>(request, response), user);
        model.addAttribute("page", page);
		if(page.getList().size()==0){
			model.addAttribute("message1","无查询结果");
		}
		return "modules/sys/tenementManagerList";
	}
	//修改或添加页面
	@RequestMapping(value = "form")
	public String form(User user, Model model) {
		//得到代理商所在的城市id
		User ares=shopAddService.userAreas(UserUtils.getUser().getId());
		//根据城市得到区域集合
		int cid=Integer.parseInt(ares.getCitys());
		List<Areas> area1 = service.showAreaListBycid(cid);
		model.addAttribute("areasList",area1);
		if(user.getId()!=null){
			TbCommunity tbCommunity=tbCommunityService.get(Integer.parseInt(user.getCommunityId()));
			model.addAttribute("community",tbCommunity);
		}
		model.addAttribute("user", user);
		return "modules/sys/tenementManagerForm";
	}
	//添加或修改物业管理员
	@RequestMapping(value = "save")
	public String save(User user, HttpServletRequest request, Model model, RedirectAttributes redirectAttributes) {
		// 修正引用赋值问题，不知道为何，Company和Office引用的一个实例地址，修改了一个，另外一个跟着修改。
		user.setCompany(new Office(user.getProvinces()));
		user.setOffice(new Office(user.getProvinces()));
		// 如果新密码为空，则不更换密码
		if (StringUtils.isNotBlank(user.getNewPassword())) {
			user.setPassword(SystemService.entryptPassword(user.getNewPassword()));
		}
		if (!beanValidator(model, user)){
			return form(user, model);
		}
		if (!"true".equals(checkLoginName(user.getOldLoginName(), user.getLoginName()))){
			addMessage(model, "保存用户'" + user.getLoginName() + "'失败，登录名已存在");
			return form(user, model);
		}
		// 保存用户信息
		tenementManagerService.saveUser(user);
		// 清除当前用户缓存
		if (user.getLoginName().equals(UserUtils.getUser().getLoginName())){
			UserUtils.clearCache();
			//UserUtils.getCacheMap().clear();
		}
		addMessage(redirectAttributes, "保存商物业管理员户'" + user.getLoginName() + "'成功");
		return "redirect:" + adminPath + "/sys/tenementManagerController/tenenmentManagerList";
	}
	/**
	 * 验证登录名是否有效
	 * @param oldLoginName
	 * @param loginName
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "checkLoginName")
	public String checkLoginName(String oldLoginName, String loginName) {
		if (loginName !=null && loginName.equals(oldLoginName)) {
			return "true";
		} else if (loginName !=null && systemService.getUserByLoginName(loginName) == null) {
			return "true";
		}
		return "false";
	}
	/**
	 * 删除物业管理员
	 * @param user
	 * @param redirectAttributes
	 * @return
	 */
	@RequestMapping(value = "delete")
	public String delete(User user, RedirectAttributes redirectAttributes) {
		if(Global.isDemoMode()){
			addMessage(redirectAttributes, "演示模式，不允许操作！");
			return "redirect:" + adminPath + "/sys/tenementManagerController/tenenmentManagerList";
		}
		if (UserUtils.getUser().getId().equals(user.getId())){
			addMessage(redirectAttributes, "删除物业管理员失败, 不允许删除当前用户");
		}else if (User.isAdmin(user.getId())){
			addMessage(redirectAttributes, "删除物业管理员失败, 不允许删除超级管理员用户");
		}else{
			systemService.deleteUser(user);
			addMessage(redirectAttributes, "删除物业管理员成功");
		}
		return "redirect:" + adminPath + "/sys/tenementManagerController/tenenmentManagerList";
	}



}
